Skip to content

Refactor/better error codes#18

Open
Katze719 wants to merge 13 commits intomainfrom
refactor/better-error-codes
Open

Refactor/better error codes#18
Katze719 wants to merge 13 commits intomainfrom
refactor/better-error-codes

Conversation

@Katze719
Copy link
Member

No description provided.

- Introduced a new structure for status codes in status_codes.h, categorizing errors into Configuration, Connection, Io, Control, and Monitor.
- Removed the obsolete status_codes.hpp file, consolidating functionality into status_codes.h.
- Updated validation functions in validation.hpp to utilize the new status code structure for improved error handling.
- Adjusted documentation in serial_abort_read.h and serial_abort_write.h to reflect changes in status code usage.
- Changed ValueType from std::int32_t to std::int64_t for improved range.
- Replaced the make function with a GenCode template structure for generating error codes, enhancing clarity and maintainability.
- Updated error codes in Configuration, Connection, Io, Control, and Monitor categories to utilize the new GenCode structure.
- Added an explicit conversion operator to the GenCode template for improved usability.
- Updated status code definitions in Configuration, Connection, Io, Control, and Monitor categories to utilize the GenCode structure directly, enhancing clarity and maintainability.
- Moved status code definitions into a new cpp_core::status_codes namespace for better modularity.
- Introduced a detail namespace to encapsulate internal structures, enhancing code clarity.
- Updated references to ValueType and CategoryBase to reflect the new namespace structure, improving maintainability.
@Katze719 Katze719 marked this pull request as ready for review March 18, 2026 08:29
@Katze719 Katze719 requested a review from Mqxx March 18, 2026 08:29
- Introduced a new header file status_code.h to define a structured approach for status codes within the cpp_core::status_codes namespace.
- Implemented a CategoryBase template for generating error codes, enhancing clarity and maintainability.
- Organized status codes into categories: Configuration, Connection, Io, and Control, each with specific error codes for improved usability.
- Changed the CategoryBase template to accept a derived type instead of a category value, enhancing type safety and clarity.
- Updated the Configuration, Connection, Io, and Control structs to define their category codes directly, improving maintainability and readability of status code definitions.
- Changed the initialization of kCategoryMultiplier from an assignment to a uniform initialization syntax, enhancing code consistency and clarity.
@Katze719 Katze719 marked this pull request as draft March 18, 2026 18:57
- Introduced CPP_CORE_STATUS_CATEGORY and CPP_CORE_CODE macros for streamlined status code definitions, improving maintainability and readability.
- Added isError and isSuccess functions for better error handling.
- Updated the CategoryBase template to support compile-time code generation, enhancing type safety and clarity in status code management.
@Katze719 Katze719 marked this pull request as ready for review March 18, 2026 19:11
@Katze719 Katze719 requested a review from Mqxx March 18, 2026 19:12
- Moved isError, isSuccess, and belongsTo functions into the StatusCode struct for better encapsulation and organization.
- Updated function signatures to use detail::ValueType for consistency with the new structure.
- Removed redundant definitions from the detail namespace, enhancing code clarity and maintainability.
@Katze719 Katze719 force-pushed the refactor/better-error-codes branch from 2c35c6e to 2b63c69 Compare March 18, 2026 19:27
- Refactored the Code template to accept a Category type, improving type safety and clarity.
- Introduced new member functions for retrieving the name and category of status codes.
- Replaced macro-based status code definitions with structured definitions for Configuration, Connection, and Io categories, enhancing maintainability and readability.
Katze719 and others added 2 commits March 20, 2026 07:34
Co-authored-by: Mqx <62719703+Mqxx@users.noreply.github.com>
- Added static assertions to enforce constraints on category and local codes, improving safety against negative values and overflow.
- Introduced a new test category, FakeCategory, to validate the computation of status codes, ensuring correct behavior across edge cases and preventing overflow.
- Updated test cases to cover various scenarios, enhancing the robustness of the status code generation logic.
Comment on lines +141 to +179
namespace cpp_core::status_codes::detail::tests
{

template <int64_t CatCode> struct FakeCategory : CategoryBase<FakeCategory<CatCode>>
{
static constexpr ValueType kCategoryCode = CatCode;

template <ValueType Local> static consteval auto call() -> ValueType
{
return CategoryBase<FakeCategory>::template computeValue<Local>();
}
};

// Formula: result == -(kCategoryCode * 100 + LocalCode)
static_assert(FakeCategory<1>::call<0>() == -100);
static_assert(FakeCategory<1>::call<42>() == -142);
static_assert(FakeCategory<3>::call<7>() == -307);

// Edge: kCategoryCode == 0 -> call<0>() produces 0 (not negative)
static_assert(FakeCategory<0>::call<0>() == 0);
static_assert(FakeCategory<0>::call<1>() == -1);

// Edge: LocalCode == 99 (max before overflow guard)
static_assert(FakeCategory<2>::call<99>() == -299);

// Consecutive codes differ by exactly -1
static_assert(FakeCategory<1>::call<1>() - FakeCategory<1>::call<0>() == -1);

// Adjacent category ranges don't overlap (last of cat N > first of cat N+1)
static_assert(FakeCategory<1>::call<99>() > FakeCategory<2>::call<0>());

// Overflow: largest safe category still produces correct results
inline constexpr ValueType kMaxSafeCat =
(std::numeric_limits<ValueType>::max() - kCategoryMultiplier + 1) / kCategoryMultiplier;
static_assert(kMaxSafeCat > 1'000'000);
static_assert(FakeCategory<kMaxSafeCat>::call<0>() == -(kMaxSafeCat * kCategoryMultiplier));
static_assert(FakeCategory<kMaxSafeCat>::call<99>() > std::numeric_limits<ValueType>::min());

} // namespace cpp_core::status_codes::detail::tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't stay in this file. Please refactor to separate test file, like we discussed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should stay in this file, like we discussed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Like I said. This is like a unit test, and not to control something platform specific.

Copy link
Member

@Mqxx Mqxx Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_asserts should only stay directly in the file if they are used to fail if the condition for the platform is not met and the compiler should fail. You use it for unit testing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants